package com.blog.service.Impl;

import com.blog.context.BaseContext;
import com.blog.dto.CategoryDTO;
import com.blog.dto.CategorySearchDTO;
import com.blog.entity.Category;
import com.blog.mapper.CategoryMapper;
import com.blog.mapper.UserCategoryMapper;
import com.blog.service.CategoryService;
import com.blog.vo.CategoryVO;
import com.blog.vo.userCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private UserCategoryMapper userCategoryMapper;

    @Override
    public List<CategoryVO> list() {
        List<CategoryVO> list = new ArrayList<>();

        List<Category> DBList = categoryMapper.select();
        //拷贝到list集合，类型为CategoryVO
        DBList.forEach(category -> {
            CategoryVO categoryVO = new CategoryVO(); //每次拷贝的时候需要创建不同的实例对象
            BeanUtils.copyProperties(category, categoryVO);
            list.add(categoryVO);
        });
        return list;
    }

    @Override
    public List<CategoryVO> listByUId(Long Uid) {
        List<CategoryVO> list = new ArrayList<>();
        //先查该用户表tb_user_category对应的类别ID数组
        List<Long> categoryList = userCategoryMapper.select(Uid);
        //根据数组去查其对应的tb_category对应的类别：批量查询
        List<Category> categories = categoryMapper.selectBatchIds(categoryList);
        categories.forEach(category -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            list.add(categoryVO);
        });
        return list;
    }

    @Override
    public List<CategoryVO> SearchCategory(CategorySearchDTO categorySearchDTO) {
        List<CategoryVO> list = new ArrayList<>();
        //根据用户ID获取到所有类别的ID
        List<Long> categoryListIds = userCategoryMapper.select(categorySearchDTO.getUid());

        //根据模糊查询的值查询复合条件的类别
        List<Category> categoryList = categoryMapper.selectBatchIdsAndName(categoryListIds, categorySearchDTO.getValue());
        categoryList.forEach(category -> {
            CategoryVO categoryVO = new CategoryVO();
            categoryVO.setId(category.getId());
            categoryVO.setType(category.getType());
            list.add(categoryVO);
        });
        return list;
    }

    @Override
    public CategoryVO listById(Long id) {
        Category category = categoryMapper.selectById(id);
        CategoryVO categoryVO = new CategoryVO();
        BeanUtils.copyProperties(category,categoryVO);
        return categoryVO;
    }

    @Override
    public boolean AddCategory(String value) {
        //先查看Category的value是否存在
        //把输入的值的英文转化为英文大写
        Long currentId = BaseContext.getCurrentId();
        String upperValue = value.toUpperCase();
        Category category = categoryMapper.selectByName(upperValue);
        if (category != null){
            //如果存在，则直接在表tb_user_category添加当前用户ID和类别ID的数据
            Long cid = category.getId();
            //先查看该表有没有相同的数据
            Long count = userCategoryMapper.selectByUidAndCid(cid, currentId);
            //数据存在
            if (count != 0) return false;
            //数据不存在，插入数据
            userCategoryMapper.insert(cid,currentId);
            return true;
        } else {
            //如果不存在，则先在表先查看Category的添加数据(返回生成的类别ID)
            Category InsertCategory = new Category();
            InsertCategory.setType(upperValue);
            categoryMapper.InsertCategory(InsertCategory);
            //然后在表tb_user_category添加当前用户ID和类别ID的数据
            userCategoryMapper.insert(InsertCategory.getId(),currentId);
        }
        return true;
    }

    @Override
    public boolean EditCategory(CategoryDTO categoryDTO) {
        //先去查是否有这个Type
        Category category = categoryMapper.selectByName(categoryDTO.getType().toUpperCase());
        //有这个类返回FALSE，修改失败
        if (category != null)return false;
        //没有返回true，修改成功
        categoryDTO.setType(categoryDTO.getType().toUpperCase());
        categoryMapper.update(categoryDTO);
        return true;
    }

    @Override
    public boolean DeleteCategory(Long id) {
        // TODO: 需要完善

        //先去判断这个类别的文章表：tb_ategory_Article 是否存在

        //这个类别的作品类别表：tb_works_category 是否存在



        //上面如果存在的话不给删除这个类别，反之可以删除

        //可以删除：
        //先删除用户类别表：tb_user_category的数据

        //再删除类别表：tb_category的数据

        return false;
    }

    @Override
    public List<userCategoryVO> userCategoryList(Long uid) {
        List<userCategoryVO> userCategoryVOS = new ArrayList<>();
        List<CategoryVO> categoryVOList = categoryMapper.selectCategoryList(uid);
        categoryVOList.forEach(categoryVO -> {
            userCategoryVO userCategoryVO = new userCategoryVO();
            //先查询每个类别的文章数量
            Long count = categoryMapper.countArticle(categoryVO.getId());
            BeanUtils.copyProperties(categoryVO,userCategoryVO);
            userCategoryVO.setTotal(count);
            //写入userCategoryVO属性，添加到数组中
            userCategoryVOS.add(userCategoryVO);
        });
        return userCategoryVOS;
    }

    @Override
    public Long getCategoryTotal(Long uid) {
        return userCategoryMapper.countCategory(uid);
    }
}
